package com.example.demo.algorithm.distribute_lock;

import lombok.SneakyThrows;
import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

public class RedisLock {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    RedissonClient redissonClient;

    @SneakyThrows
    public void test(){
//        redisTemplate.opsForValue().set("key", "value");
        RLock rLock = redissonClient.getLock("key");
        RLock rLock1 = redissonClient.getLock("key1");
        rLock.tryLock(1000, TimeUnit.SECONDS);

        RedissonRedLock redLock = new RedissonRedLock(rLock, rLock1);
        redLock.lock();


    }
}
